package cn.tomoki.myapplication;

import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import cn.tomoki.adapters.ListViewAdapter;
import cn.tomoki.beans.ItemBean;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import static cn.tomoki.myapplication.Utils.classroomUrl;
import static cn.tomoki.myapplication.Utils.flush;
import static cn.tomoki.myapplication.Utils.myUrl;

//出库历史详情页面
public class OutHistoryDetalisActivity extends AppCompatActivity {

    private Button testON, testOFF;
    private ImageView productDetailsImage;  //产品图片
    private TextView personalTextId;//产品名称
    private TextView personalText2_2;//轴直径
    private TextView personalText3_3;//盘面直径
    private TextView personalText4_4;//内高
    private TextView personalText5_5;//操作
    private TextView personalText6_6;//出库数量
    private TextView personalText7_7;//操作时间
    private TextView tv_cancel;//取消出库
    private String mark;   //流水单
    private String back;    //退还状态
    //日期格式
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.out_history_detalis);

        Intent it = getIntent();
        mark = it.getStringExtra("mark");    //获取上个Activity传过来的流水单
        back = it.getStringExtra("back");   //退还状态
        testON = findViewById(R.id.testON);
        testOFF = findViewById(R.id.testOFF);
        testON.setVisibility(View.GONE);
        testOFF.setVisibility(View.GONE);

        productDetailsImage = findViewById(R.id.productDetailsImage);
        personalTextId = findViewById(R.id.personalTextId);
        personalText2_2 = findViewById(R.id.personalText2_2);
        personalText3_3 = findViewById(R.id.personalText3_3);
        personalText4_4 = findViewById(R.id.personalText4_4);
        personalText5_5 = findViewById(R.id.personalText5_5);
        personalText6_6 = findViewById(R.id.personalText6_6);
        personalText7_7 = findViewById(R.id.personalText7_7);

        tv_cancel = findViewById(R.id.tv_cancel);
        initData(mark);

        //返回按钮
        ImageButton myBreak = findViewById(R.id.breakButton);
        myBreak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        testON.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_cancel.setVisibility(View.VISIBLE);
            }
        });
        testOFF.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tv_cancel.setVisibility(View.GONE);
            }
        });

        tv_cancel.setOnClickListener(new View.OnClickListener() {   //退还
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(OutHistoryDetalisActivity.this)
                        .setTitle("退还")
                        .setCancelable(false)
                        .setMessage("此操作无法撤回，确定要退还吗?")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {  //确定后
                                back(mark); //执行退还操作
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        }).show();

            }
        });
    }

    public void back(String back) {  //点击退还后作出的响应
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    FormBody.Builder params = new FormBody.Builder();
                    params.add("mark", mark);
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url(Utils.classroomUrl+"/outuser/back")
                            .post(params.build())
                            .build();//创造http请求
                    Response response = client.newCall(request).execute();
                    String result = response.body().string();
                    JSONObject jsonObject = new JSONObject(result);
                    String msg = jsonObject.getString("msg");
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            personalText5_5.setText("退还");  //更新操作
                            tv_cancel.setText("已退还");
                            tv_cancel.setEnabled(false);   //无法点击
                            flush(personalText5_5);
                            flush(tv_cancel);   //更新操作
                            Toast.makeText(OutHistoryDetalisActivity.this, msg, Toast.LENGTH_SHORT).show();
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(OutHistoryDetalisActivity.this, "网络连接失败", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        }).start();
    }

    public void initData(String mark) {
        //接受后端数据
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    FormBody.Builder params = new FormBody.Builder();
                    params.add("mark", mark);
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url(classroomUrl+"/outuser/history/detail")
                            .post(params.build())
                            .build();//创造http请求
                    Response response = client.newCall(request).execute();
                    String result = response.body().string();
                    JSONObject jsonObject = new JSONObject(result);
                    JSONObject object = new JSONObject(jsonObject.getString("pushDetail"));
                    String name = object.getString("name");
                    String innerwidth = object.getString("innerwidth");
                    String outerwidth = object.getString("outerwidth");
                    String tbody = object.getString("tbody");
                    String number = object.getString("number");
                    String time = object.getString("time");
                    back = object.getString("back");

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            personalTextId.setText(name);
                            personalText2_2.setText(innerwidth);
                            personalText3_3.setText(outerwidth);
                            personalText4_4.setText(tbody);

                            personalText6_6.setText(number);
                            personalText7_7.setText(time);
                            if (back.equals("1")) {
                                personalText5_5.setText("退还");
                                tv_cancel.setText("已退还");
                                tv_cancel.setEnabled(false);   //无法点击
                            } else {
                                tv_cancel.setEnabled(true);
                                personalText5_5.setText("出库");
                                System.out.println(simpleDateFormat.format(new Date(System.currentTimeMillis())));
                                Date date = null;
                                try {
                                    date = simpleDateFormat.parse(time);
                                } catch (ParseException e) {
                                    e.printStackTrace();
                                }
//                                long nowTime = System.currentTimeMillis() + 8 * 60 * 60 * 1000;
                        long nowTime = System.currentTimeMillis();
                                long startTime = date.getTime();
                                System.out.println(simpleDateFormat.format(startTime));
                                if (nowTime - startTime > 30 * 60 * 1000) {
                                    tv_cancel.setVisibility(View.GONE);
                                }
                            }
                        }
                    });

                } catch (Exception e) {
                    e.printStackTrace();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(OutHistoryDetalisActivity.this, "网络连接失败", Toast.LENGTH_SHORT).show();
                        }
                    });

                }
            }
        }).start();
    }

}